<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
    protected $resultSetType = 'collection';
    /*买家显示---连接代理商获取前缀*/
    public function showBuysers(){
        $res = model('user')->alias('u')
            ->join('agency','agency.id = u.agencyid','left')
            ->group('u.id')
            ->field("u.*,agency.prefix")
            ->select()
            ->toArray();
        return $res;
    }
    /*地图功能*/
    /*public function DisplayArea($AgentSel){
        $array = array();
        for($i=0;$i<count($AgentSel);$i++){
            if(empty($AgentSel[$i]['location'])){
                return $AgentSel;
            }else{
                $location = $AgentSel[$i]['location'];
                $res = nibm($location);
                //将地区字符串存放在一个二维数组里，方便后面将两个数组合并为一个数组
                $array[$i] = [$res];
            }
        }
        $array2 = array();
        //把代理商信息二维数组遍历，再把地区的二维数组以$k值分开，合并为一个二维数组
        foreach($AgentSel as $k=>$r){
            $array2[] = array_merge($r,$array[$k]);
        }
        return $array2;
    }*/
    /*买家添加*/
    public function useradd($name,$intergral,$money,$iphone,$email,$account,$root,$path,$hjwd){
        $code  = new User;
        $randomcode = $code->randomcode();
        $db = model('user')->insert([
            'name'      =>  $name,
            'intergral' =>  $intergral,
            'money'     =>  $money,
            'iphone'    =>  $iphone,
            'email'     =>  $email,
            'account'   =>  $account,
            'root'      =>  md5($root.$randomcode),
            'randomcode' => $randomcode,
            'path'      =>  $path,
            'location'  =>  $hjwd
        ]);
        if($db){
            return 1;
        }
        else{
            return 0;
        }
    }
    /*买家修改*/
    public function usermod($id,$name,$intergral,$money,$iphone,$email,$path,$hjwd){
        $user      = new User;
        $user->save([
            'name'      => $name,
            'intergral' => $intergral,
            'money'     => $money,
            'iphone'    => $iphone,
            'email'     => $email,
            'path'      => $path,
            'location'  => $hjwd
        ],['id' => $id]);
        return 1;
    }

    public function randomcode(){
        $pattern = '1234567890abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        for($i=0,$key='';$i<20;$i++)
        {
            $key .= $pattern{mt_rand(0,60)};
        }
        return $key;
    }
    //根据id查询团长名字和上级id
    public function oneData($id){
        $res=$this->alias('a')
            ->join("useradmin",'a.id=useradmin.userid','left')
            ->where(['ismanage'=>1,'a.id'=>$id])
            ->field('account,adminid,name')
            ->find();
        return $res;
    }
    //查询既是团长又是代理商的店铺id
    public function agencyids(){
        $res=$this->where(['agencyid'=>['not in',[0]],'ismanage'=>1])->field('agencyid')->select();
        $arr=[];
        foreach ($res as $v){
            $arr[]=$v['agencyid'];
        }
        return $arr;
    }
    //计算总数
    public function zong(){
        $res=$this->field('count(id) as count')->find();
        return $res['count'];
    }
    //是否团员
    public function ismanage($id){
        $res=$this->where('id',$id)->update(['ismanage'=>1]);
        return $res;
    }
    //获取今天的注册人数
    public function todaynumber(){
        $res=$this->whereTime('times', 'today')->field('count(id) as count ')->find();
        return $res['count'];

    }
    //获取最近一周的注册人数
    public function weeknumber(){
        $res=$this->whereTime('times', 'week')->field('count(id) as count ')->find();
        return $res['count'];
    }
    //获取最近一月的注册人数
    public function monthnumber(){
        $res=$this->whereTime('times', 'month')->field('count(id) as count ')->find();
        return $res['count'];
    }
}